﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
using Microsoft.Office.Interop.Excel;


namespace GUI
{
    public partial class FormDSPhieuNhap : Window
    {
        private static int co;
        public  int Co
        {
            get { return co; }
            set { co = value; }
        }

        private static int ma_PN;
        public int Ma_PN
        {
            get { return ma_PN; }
            set { ma_PN = value; }
        }

        private static DateTime ngayNhap_PN;
        public DateTime NgayNhap_PN
        {
            get { return ngayNhap_PN; }
            set { ngayNhap_PN = value; }
        }

        private static int maDH_PN;
        public int MaDH_PN
        {
            get { return maDH_PN; }
            set { maDH_PN = value; }
        }

        private static int maNV_PN;
        public int MaNV_PN
        {
            get { return maNV_PN ;}
            set { maNV_PN = value ;}
        }

        public static string ghiChu_PN;
        public string GhiChu_PN
        {
            get { return ghiChu_PN; }
            set { ghiChu_PN = value; }
        }

        public FormDSPhieuNhap()
        {
            InitializeComponent();
            hoiThoat = false;
        }


        private void FormDSPhieuNhap_Load(object sender, EventArgs e)
        {
            Co = 0;
            dtpBatDau.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            PhieuNhapBUS bus = new PhieuNhapBUS();
            List<PhieuNhapDTO> ds = new List<PhieuNhapDTO>();
            ds = bus.LayDSPhieuNhap();

            dataGridViewDSPN.Rows.Clear();
            for (int i = 0; i < ds.Count(); i++)
                dataGridViewDSPN.Rows.Add(ds[i].MaPN, ds[i].NgayNhap, ds[i].MaDH, ds[i].MaNV, ds[i].GhiChu);
        }


        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            DateTime F = dtpBatDau.Value;
            DateTime T = dtpKetThuc.Value;
          
            PhieuNhapBUS bus = new PhieuNhapBUS();
            List<PhieuNhapDTO> ds;
            ds = bus.LayDSPNTheoNgay(F,T);

            dataGridViewDSPN.Rows.Clear();
            for (int i = 0; i < ds.Count(); i++)
                dataGridViewDSPN.Rows.Add(ds[i].MaPN, ds[i].NgayNhap, ds[i].MaDH, ds[i].MaNV, ds[i].GhiChu);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int row = dataGridViewDSPN.CurrentRow.Index;
            int maPN = int.Parse(dataGridViewDSPN[0, row].Value.ToString());
            int maDH = int.Parse(dataGridViewDSPN[2, row].Value.ToString());
            PhieuNhapBUS bus = new PhieuNhapBUS();
            if (MessageBox.Show(this, "Xóa phiếu nhập có MaPN:  " + maPN + "?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
   
                //Bot san pham da them
                CTPhieuNhapBUS chitiet = new CTPhieuNhapBUS();
                List<CTPhieuNhapDTO> dsct = chitiet.LayCTPhieuNhap(maPN);

                SPBUS spbus = new SPBUS();

                CTDHBUS ctdhbus = new CTDHBUS();
                for (int i = 0; i < dsct.Count(); i++)
                {
                    spbus.CapNhatThemSanPham(dsct[i].MaSP, -dsct[i].SoLuong);
                    BUS.SPBUS.capnhatsodu(dsct[i].MaSP, -dsct[i].SoLuong, this.dtpBatDau.Value);
                    ctdhbus.CapNhatSoLuongConLai(maDH, dsct[i].MaSP, dsct[i].SoLuong);
                }

                // Xoa chi tiet phieu nhap
                chitiet.XoaChiTiet(maPN);
                MessageBox.Show(this, "Xóa thành công!", "Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);

                // Xoa phieu nhap
                bus.XoaPhieuNhap(maPN);

                FormDSPhieuNhap_Load(null, null);

            }


        }

        private void button5_Click(object sender, EventArgs e)
        {
            FormThemPhieuNhap frm = new FormThemPhieuNhap();
            frm.MdiParent = this.MdiParent;
            frm.Show();
            FormDSPhieuNhap_Load(null, null);
        }

        private void dataGridViewDSPN_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Co = 1;
            int row = dataGridViewDSPN.CurrentRow.Index;
            Ma_PN = int.Parse(dataGridViewDSPN[0, row].Value.ToString());
            NgayNhap_PN = DateTime.Parse(dataGridViewDSPN[1, row].Value.ToString());
            MaDH_PN = int.Parse(dataGridViewDSPN[2, row].Value.ToString());
            MaNV_PN = int.Parse(dataGridViewDSPN[3, row].Value.ToString());
            GhiChu_PN = dataGridViewDSPN[3, row].Value.ToString();
            FormSuaPhieuNhap frm = new FormSuaPhieuNhap();
            frm.MdiParent = this.MdiParent;
            frm.Show();
            FormDSPhieuNhap_Load(null, null);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dataGridViewDSPN_CellDoubleClick(null,null);
            FormDSPhieuNhap_Load(null, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Application excelApp = null;
            try
            {
                // instantiating the excel application class
                object misValue = System.Reflection.Missing.Value;
                excelApp = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook currentWorkbook = excelApp.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel.Worksheet currentWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)currentWorkbook.ActiveSheet;
                currentWorksheet.Columns.ColumnWidth = 18;
                if (dataGridViewDSPN.Rows.Count > 0)
                {
                    currentWorksheet.Cells[1, 1] = DateTime.Today.ToString("dd/MM/yyyy");
                    Microsoft.Office.Interop.Excel.Range Ngay = currentWorksheet.get_Range("A1");
                    Ngay.Font.Bold = true;
                    int i = 1;
                    foreach (DataGridViewColumn dgviewColumn in dataGridViewDSPN.Columns)
                    {
                        // Excel work sheet indexing starts with 1
                        currentWorksheet.Cells[2, i] = dataGridViewDSPN.Columns[i-1].HeaderText;
                        ++i;
                    }
                    Microsoft.Office.Interop.Excel.Range headerColumnRange = currentWorksheet.get_Range("A2", "G2");
                    headerColumnRange.Font.Bold = true;
                    headerColumnRange.Font.Color = 0xFF0000;
                    //headerColumnRange.EntireColumn.AutoFit();
                    int rowIndex = 0;
                    for (rowIndex = 0; rowIndex < dataGridViewDSPN.Rows.Count ; rowIndex++)
                    {
                        DataGridViewRow dgRow = dataGridViewDSPN.Rows[rowIndex];
                        for (int cellIndex = 0; cellIndex < dgRow.Cells.Count ; cellIndex++)
                        {
                            currentWorksheet.Cells[rowIndex + 3, cellIndex +1 ] = dgRow.Cells[cellIndex].Value;
                        }
                    }
                    Microsoft.Office.Interop.Excel.Range fullTextRange = currentWorksheet.get_Range("A1", "G" + (rowIndex + 1).ToString());
                    fullTextRange.WrapText = true;
                    fullTextRange.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft;
                }
                else
                {
                    string timeStamp = DateTime.Now.ToString("s");
                    timeStamp = timeStamp.Replace(':', '-');
                    timeStamp = timeStamp.Replace("T", "__");
                    currentWorksheet.Cells[1, 1] = timeStamp;
                    currentWorksheet.Cells[1, 2] = "No error occured";
                }
                using (SaveFileDialog exportSaveFileDialog = new SaveFileDialog())
                {
                    exportSaveFileDialog.Title = "Select Excel File";
                    exportSaveFileDialog.Filter = "Microsoft Office Excel Workbook(*.xlsx)|*.xlsx";

                    if (DialogResult.OK == exportSaveFileDialog.ShowDialog())
                    {
                        string fullFileName = exportSaveFileDialog.FileName;
                        // currentWorkbook.SaveCopyAs(fullFileName);
                        // indicating that we already saved the workbook, otherwise call to Quit() will pop up
                        // the save file dialogue box

                        currentWorkbook.SaveAs(fullFileName, Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, System.Reflection.Missing.Value, misValue, false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlUserResolution, true, misValue, misValue, misValue);
                        currentWorkbook.Saved = true;
                        MessageBox.Show("Exported successfull", "Exported to Excel", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (excelApp != null)
                {
                    excelApp.Quit();
                }
            }
        }

       
    }
}
